﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiAPPLocationsDataGrid
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CUiAPPLocationsDataGrid
{
  private DataGrid m_locationsDataGrid;
  private CApAPPTaskManager m_taskManager;
  private bool m_externalSelectionMode;

  public CUiAPPLocationsDataGrid(DataGrid i_dataGrid, CApAPPTaskManager i_taskManager)
  {
    this.m_locationsDataGrid = i_dataGrid;
    this.m_taskManager = i_taskManager;
    this.RegisterSelectionEvents();
  }

  ~CUiAPPLocationsDataGrid() => this.UnregisterSelectionEvents();

  public bool ExternalSelectionMode
  {
    get => this.m_externalSelectionMode;
    set => this.m_externalSelectionMode = value;
  }

  public void RefreshAll() => this.UpdateView();

  public void Refresh(CApAPPWaypointProperty i_property)
  {
    IEnumerator enumerator = this.m_locationsDataGrid.ItemsSource.GetEnumerator();
    enumerator.Reset();
    while (enumerator.MoveNext())
      (enumerator.Current as CApAPPWaypoint).PropertyDidChanged(i_property);
  }

  public void UpdateView()
  {
    ObservableCollection<CApAPPWaypoint> observableCollection = new ObservableCollection<CApAPPWaypoint>();
    observableCollection.Clear();
    CApAPPWaypointCollectionView waypointCollectionView = new CApAPPWaypointCollectionView((IList) observableCollection);
    this.m_locationsDataGrid.ItemsSource = (IEnumerable) waypointCollectionView;
    for (int index = 0; index < this.m_taskManager.Tasks.Count; ++index)
    {
      for (int i_index = 0; i_index < this.m_taskManager.Tasks[index].CountWaypoints(); ++i_index)
      {
        CApAPPWaypoint capAppWaypoint = this.m_taskManager.Tasks[index].Waypoint(i_index);
        observableCollection.Add(capAppWaypoint);
      }
    }
    waypointCollectionView.GroupDescriptions.Add((GroupDescription) new PropertyGroupDescription("Task"));
    this.m_locationsDataGrid.Refresh();
  }

  public void UnSelectionLocations() => this.m_locationsDataGrid.SelectedItem = (object) null;

  public void SelectAllExternalOperations()
  {
    TxObjectList objects = new TxObjectList();
    for (int index = 0; index < this.m_taskManager.CountTasks(); ++index)
      ((Collection<ITxObject>) objects).Add((ITxObject) this.m_taskManager.Tasks[index].Operation);
    if (((Collection<ITxObject>) objects).Count == 0)
      return;
    this.SetExternalSelection(objects);
  }

  public void SelectLocations(TxObjectList i_selectedObjects)
  {
    foreach (ITxObject iSelectedObject in (Collection<ITxObject>) i_selectedObjects)
    {
      int num = 0;
      CApAPPWaypoint capAppWaypoint = (CApAPPWaypoint) null;
      int o_index = 0;
      for (int index1 = 0; index1 < this.m_taskManager.CountTasks() && capAppWaypoint == null; ++index1)
      {
        if (iSelectedObject == this.m_taskManager.Tasks[index1].Operation)
          this.RetrieveTaskFromIndexNode(index1, ref o_index);
        TxObjectList allLocations = this.m_taskManager.Tasks[index1].GetAllLocations();
        for (int index2 = 0; index2 < ((Collection<ITxObject>) allLocations).Count; ++index2)
        {
          if (iSelectedObject == ((Collection<ITxObject>) allLocations)[index2])
          {
            capAppWaypoint = this.m_taskManager.Tasks[index1].Waypoint(index2);
            break;
          }
          ++num;
        }
      }
      if (capAppWaypoint != null)
        this.m_locationsDataGrid.SelectedIndex = num;
    }
  }

  public void DeselectLocations(TxObjectList locations)
  {
  }

  public void SelectionChanged(int i_row)
  {
    this.ExternalSelectionMode = true;
    IList selectedItems = this.m_locationsDataGrid.SelectedItems;
    TxObjectList objects = new TxObjectList();
    if (selectedItems != null)
    {
      if (selectedItems.Count == 0)
      {
        ITxOperation o_operation = (ITxOperation) null;
        if (this.RetrieveNodeOperation(i_row, ref o_operation))
          ((Collection<ITxObject>) objects).Add((ITxObject) o_operation);
      }
      else
      {
        List<CApAPPWaypoint> capAppWaypointList = new List<CApAPPWaypoint>();
        for (int index = 0; index < selectedItems.Count; ++index)
          capAppWaypointList.Add(selectedItems[index] as CApAPPWaypoint);
        IEnumerator enumerator = this.m_locationsDataGrid.ItemsSource.GetEnumerator();
        for (int index1 = 0; index1 < capAppWaypointList.Count; ++index1)
        {
          enumerator.Reset();
          int index2 = 0;
          while (enumerator.MoveNext())
          {
            CApAPPWaypoint current = enumerator.Current as CApAPPWaypoint;
            if (enumerator.Current == capAppWaypointList[index1])
            {
              ITxObject itxObject = this.RetrieveWaypointFromLocation(index2);
              ((Collection<ITxObject>) objects).Add(itxObject);
              break;
            }
            ++index2;
          }
        }
      }
      if (((Collection<ITxObject>) objects).Count > 0)
        this.SetExternalSelection(objects);
    }
    this.ExternalSelectionMode = false;
  }

  public ITxObject RetrieveWaypointFromLocation(int index)
  {
    int index1 = 0;
    int num = 0;
    ITxObject itxObject = (ITxObject) null;
    for (; index1 < this.m_taskManager.CountTasks(); ++index1)
    {
      if (index >= num + this.m_taskManager.Tasks[index1].CountWaypoints())
      {
        num += this.m_taskManager.Tasks[index1].CountWaypoints();
      }
      else
      {
        if (((Collection<ITxObject>) this.m_taskManager.Tasks[index1].GetAllLocations()).Count > index - num)
        {
          itxObject = ((Collection<ITxObject>) this.m_taskManager.Tasks[index1].GetAllLocations())[index - num];
          break;
        }
        break;
      }
    }
    return itxObject;
  }

  public void RegisterSelectionEvents()
  {
    try
    {
      TxSelection activeSelection = TxApplication.ActiveSelection;
      activeSelection.ItemsAdded += new TxSelection_ItemsAddedEventHandler(this.OnSelectionAdded);
      activeSelection.ItemsRemoved += new TxSelection_ItemsRemovedEventHandler(this.OnSelectionRemoved);
      activeSelection.ItemsSet += new TxSelection_ItemsSetEventHandler(this.OnSelectionSet);
      activeSelection.Cleared += new TxSelection_ClearedEventHandler(this.OnSelectionCleared);
    }
    catch (Exception ex)
    {
    }
  }

  private void OnSelectionSet(object sender, TxSelection_ItemsSetEventArgs args)
  {
    this.ExternalSelectionMode = true;
    this.SelectLocations(args.ObjectList);
    this.ExternalSelectionMode = false;
  }

  public void UnregisterSelectionEvents()
  {
    try
    {
      TxSelection activeSelection = TxApplication.ActiveSelection;
      activeSelection.ItemsAdded -= new TxSelection_ItemsAddedEventHandler(this.OnSelectionAdded);
      activeSelection.ItemsRemoved -= new TxSelection_ItemsRemovedEventHandler(this.OnSelectionRemoved);
      activeSelection.ItemsSet -= new TxSelection_ItemsSetEventHandler(this.OnSelectionSet);
      activeSelection.Cleared -= new TxSelection_ClearedEventHandler(this.OnSelectionCleared);
    }
    catch (Exception ex)
    {
    }
  }

  private void OnSelectionAdded(object sender, TxSelection_ItemsAddedEventArgs args)
  {
    this.ExternalSelectionMode = true;
    this.SelectLocations(args.ObjectList);
    this.ExternalSelectionMode = false;
  }

  private void OnSelectionRemoved(object sender, TxSelection_ItemsRemovedEventArgs args)
  {
    this.ExternalSelectionMode = true;
    this.DeselectLocations(args.ObjectList);
    this.ExternalSelectionMode = false;
  }

  private void OnSelectionCleared(object sender, TxSelection_ClearedEventArgs args)
  {
    this.ExternalSelectionMode = true;
    this.ExternalSelectionMode = false;
  }

  internal void SetExternalSelection(TxObjectList objects)
  {
    this.UnregisterSelectionEvents();
    TxApplication.ActiveSelection.SetItems(objects);
    this.RegisterSelectionEvents();
  }

  private bool RetrieveTaskFromIndexNode(int i_indexTask, ref int o_index)
  {
    bool flag = false;
    o_index = 0;
    for (int index = 0; index < i_indexTask; ++index)
      o_index += this.m_taskManager.Tasks[index].CountWaypoints() + 1;
    return flag;
  }

  private bool RetrieveNodeOperation(int i_row, ref ITxOperation o_operation)
  {
    bool flag = false;
    int num = 0;
    for (int index = 0; index < this.m_taskManager.CountTasks(); ++index)
    {
      if (i_row < num + this.m_taskManager.Tasks[index].CountWaypoints())
      {
        o_operation = this.m_taskManager.Tasks[index].Operation;
        flag = true;
        break;
      }
      num += this.m_taskManager.Tasks[index].CountWaypoints() + 1;
    }
    return flag;
  }
}
